package com.goldmantis.alitrip.common.config;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.*;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.contexts.SecurityContext;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author:Shmily
 * @Description:
 * @Date:Create in 9:29 2018/2/12
 */
@Configuration
@EnableWebMvc
@EnableSwagger2
@Profile({"dev"})
@ComponentScan(basePackages = "com.goldmantis.alitrip.controller")
public class SwaggerConfig {
    @Bean
    public Docket myDocket() {

        ParameterBuilder tokenPar = new ParameterBuilder();
        List<Parameter> pars = new ArrayList<Parameter>();
        tokenPar.name("Authorization").description("access token(请手动补上[bearer ]头)").modelRef(new ModelRef("string")).parameterType("header").required(false).build();
        pars.add(tokenPar.build());

        Docket docket = new Docket(DocumentationType.SWAGGER_2);

        docket.pathMapping("/rest");

        ApiInfo apiInfo = new ApiInfoBuilder()
                .title("权限控制系统API接口文档")
                .description("版权所有 金螳螂")
                .contact(new Contact("chenjindong", "www.chenjindong.com", "chenjindong@goldmantis.com"))
                .version("1.0")
                .build();
        docket.globalOperationParameters(pars);
        docket.apiInfo(apiInfo);

        //设置只生成被Api这个注解注解过的Ctrl类中有ApiOperation注解的api接口的文档
        docket.select()
                .apis(RequestHandlerSelectors.withClassAnnotation(Api.class))
                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
                .build()
                .securitySchemes(securitySchemes())
                .securityContexts(securityContexts());;
        return docket;
    }

    private List<ApiKey> securitySchemes() {
        List<ApiKey> list= new ArrayList<ApiKey>();
        list.add(new ApiKey("clientId", "客户端ID", "header"));
        list.add(new ApiKey("clientSecret", "客户端秘钥", "header"));
        list.add(new ApiKey("accessToken", "客户端访问标识", "header"));

        return list;
    }

    private List<SecurityContext> securityContexts() {

        List<SecurityContext> list= new ArrayList<SecurityContext>();
        list.add(SecurityContext.builder()
                        .securityReferences(defaultAuth())
                        .forPaths(PathSelectors.regex("/*.*"))
                        .build());
        return list;
    }

    List<SecurityReference> defaultAuth() {
        AuthorizationScope authorizationScope = new AuthorizationScope("global", "accessEverything");
        AuthorizationScope[] authorizationScopes = new AuthorizationScope[1];
        authorizationScopes[0] = authorizationScope;

        List<SecurityReference> list= new ArrayList<>();

        list.add(new SecurityReference("clientId", authorizationScopes));
        list.add(new SecurityReference("clientSecret", authorizationScopes));
        list.add(new SecurityReference("accessToken", authorizationScopes));

        return list;


    }


}